package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//分类模块
@RestController
public class CategoryController {


    @Autowired
    private CategoryService categoryService;

    //分类列表
    @GetMapping("/category/findAll")
    // 1.接收前端传来的参数
    public ResultInfo findAll() {
        //2.调用service
        List<Category> categoryList = categoryService.findAll();
        //返回结果
        return ResultInfo.success(categoryList);
    }

    //新增分类
    @PostMapping("/category")
    //1.接受前端的请求参数
    public ResultInfo save(@RequestBody Category category) {
        //2.调用service
        categoryService.save(category);
        //3.返回结果
        return ResultInfo.success();
    }

    //修改
    @PutMapping("/category")
    // 1.接收前端传来的请求参数
    public ResultInfo updateById(@RequestBody Category category) {
        // 2.调用service
        categoryService.updateById(category);
        // 3.返回结果
        return ResultInfo.success();
    }

    //删除
    @DeleteMapping("/category")
    // 1.接收参数
    public ResultInfo deleteById(Long id) {

        //int a = 1/0;

        // 2.调用service
         categoryService.deleteById(id);
        // 3.返回结果
        return ResultInfo.success();
    }

    // 分类回显
    @GetMapping("/category/list")
    // 1.接收前端的参数
    public ResultInfo findByType(Integer type) {
        // 2. 调用service
        List<Category> categoryList = categoryService.findByType(type);
        // 3.返回结果
        return ResultInfo.success(categoryList);
    }


}
